package com.hissy.common.design.create.factory;

import java.util.List;

/**
 * Created by zhangyanfei on 2019/4/2.
 *
 * @author zhangyanfei
 * @date 2019/04/02
 */
public class Factory {
    private List<AbstractPerson> abstractPersonList;

    /**
     * 单例创建
     *
     * @param abstractPersonList
     */
    public Factory(List<AbstractPerson> abstractPersonList) {
        this.abstractPersonList = abstractPersonList;
    }

    /**
     * 创建对象
     *
     * @param tag
     * @return
     */
    public AbstractPerson createPerson(String tag) {
        for (AbstractPerson abstractPerson : abstractPersonList) {
            if (abstractPerson.accept(tag)) {
                return abstractPerson;
            }
        }
        return null;
    }

    /**
     * 执行一个行为
     *
     * @param tag
     */
    public void doSomething(String tag) {
        for (AbstractPerson abstractPerson : abstractPersonList) {
            if (abstractPerson.accept(tag)) {
                abstractPerson.doSomething();
            }
        }
    }
}
